package dao

import (
	"context"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gtime"
	influxdb2 "github.com/influxdata/influxdb-client-go/v2"
	"github.com/influxdata/influxdb-client-go/v2/api/write"
	"math/rand"
	"paco_toolkit/common"
	"time"
)

type idb struct {
}

var Idb = idb{}

func (a *idb) Write() {
	token := "ykCqz1EHczvGKpowqJyNqFDc2QOxYUlRbuDkopRM3ISuUNQYYSny792FHQ5SspGfqUTOZJt7Z1zpJr2D0t63cw=="
	url := "http://121.42.24.64:8086"
	client := influxdb2.NewClient(url, token)

	org := "paco"
	bucket := "db1"
	writeAPI := client.WriteAPIBlocking(org, bucket)

	now := time.Now()
	tags := map[string]string{
		"表号": "12345678",
	}
	fields := map[string]interface{}{
		"温度": rand.Intn(30),
		"湿度": rand.Intn(30),
	}
	point := write.NewPoint("test", tags, fields, now)
	//time.Sleep(1000 * time.Millisecond) // separate points by 1 second

	if err := writeAPI.WritePoint(context.Background(), point); err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debug("point saved ok")
	}
	client.Close()
}
func (a *idb) Read() {
	token := "ykCqz1EHczvGKpowqJyNqFDc2QOxYUlRbuDkopRM3ISuUNQYYSny792FHQ5SspGfqUTOZJt7Z1zpJr2D0t63cw=="
	url := "http://121.42.24.64:8086"
	client := influxdb2.NewClient(url, token)
	org := "paco"
	bucket := "db1"

	queryAPI := client.QueryAPI(org)
	query := `from(bucket: "` + bucket + `")
				|> range(start: -10m)
				|> filter(fn: (r) => r["_measurement"] == "test")
				|> filter(fn: (r) => r["表号"] == "12345678")
				|> filter(fn: (r) => r["_field"] == "湿度")
				// |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)`
	g.Log().Line(true).Debug(query)
	results, err := queryAPI.Query(context.Background(), query)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		for results.Next() {
			g.Log().Line(true).Debugf("field:%v value: %v time:%s", results.Record().Field(), results.Record().Value(), gtime.NewFromTime(results.Record().Time().Local()).Format(common.DATE_TIME_FORMAT))
		}
		if err := results.Err(); err != nil {
			g.Log().Line(true).Error(err)
		}
	}

}

func (a *idb) ReadFiedsToRow() {
	token := "ykCqz1EHczvGKpowqJyNqFDc2QOxYUlRbuDkopRM3ISuUNQYYSny792FHQ5SspGfqUTOZJt7Z1zpJr2D0t63cw=="
	url := "http://121.42.24.64:8086"
	client := influxdb2.NewClient(url, token)
	org := "paco"
	bucket := "db1"

	queryAPI := client.QueryAPI(org)
	query := `from(bucket: "` + bucket + `")
				|> range(start: -10m)
				|> filter(fn: (r) => r._measurement == "test")
				|> filter(fn: (r) => r.表号 == "12345678")
				|> pivot(
				  rowKey:["_time"],
				  columnKey: ["_field"],
				  valueColumn: "_value"
				)`
	g.Log().Line(true).Debug(query)
	results, err := queryAPI.Query(context.Background(), query)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		for results.Next() {
			values := results.Record().Values()
			//g.Log().Line(true).Debug(vs)
			g.Log().Line(true).Debugf("表号:%s,温度:%v 湿度:%v time:%s", values["表号"], values["温度"], values["湿度"], gtime.NewFromTime(results.Record().Time().Local()).Format(common.DATE_TIME_FORMAT))
			//for k, v := range values {
			//	g.Log().Line(true).Debugf("key:%s,value=%v", k, v)
			//}

		}
		if err := results.Err(); err != nil {
			g.Log().Line(true).Error(err)
		}
	}

	//results, err := queryAPI.QueryRaw(context.Background(), query, influxdb2.DefaultDialect())
	//if err != nil {
	//	g.Log().Line(true).Error(err)
	//} else {
	//	g.Log().Line(true).Debug(results)
	//}

}
